<template>
	<div id="app">

		<router-view />
	</div>
</template>
<script>
	import axios from "axios"

	export default {
		data() {
			return {
				t1: 0,
				baseUrl: require("./js/baseUrl").baseUrl,
				userId: 0
			}

		},
		methods: {
			async loginout() {
				//localStorage.setItem("loginout", "test"+Math.random())
				await this.$http.post(this.baseUrl + "SchoolSys/user/logout", {
					userId: Number(sessionStorage.getItem("userId"))
				}).then(res => {
					//localStorage.setItem("res", 'ressssss')
				}).catch(res=>{
					//localStorage.setItem("erro",'eeee')
				})
			},
			beforeunloadFn() {
				var d = new Date()
				this.t1 = d.getTime()
			},
			unloadFn() {
				var d = new Date()
				var t2 = d.getTime()
				//localStorage.setItem('unloadFn', t2 - this.t1)
				if (t2 - this.t1 < 5) {
					this.loginout()
				}

			},
		},
		mounted() {
			this.userId = sessionStorage.getItem("userId")
			if(window.addEventListener){
				window.addEventListener("beforeunload", this.beforeunloadFn)
				window.addEventListener("unload", this.unloadFn)
			}else{
				window.attachEvent("onbeforeunload", this.beforeunloadFn)
				window.attachEvent("onunload", this.unloadFn)
			}
			
			
			

			if (sessionStorage.getItem("username")) {
				//显示首页
				this.$router.push("/homepage")
			} else {
				this.$router.push("/login")
			}


		}
	}

</script>

<style lang="less">
	#app {
		font-family: Avenir, Helvetica, Arial, sans-serif;
		-webkit-font-smoothing: antialiased;
		-moz-osx-font-smoothing: grayscale;
		color: #2c3e50;
	}

	nav {
		padding: 30px;

		a {
			font-weight: bold;
			color: #2c3e50;

			&.router-link-exact-active {
				color: #42b983;
			}
		}
	}
</style>